<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<!-- HTML file produced from file: manual.tex --
 -- using Hyperlatex v 2.3.1 (c) Otfried Cheong--
 -- on Emacs 21.4 (patch 12) "Portable Code" XEmacs Lucid, Wed Jun  2 18:57:01 2004 -->
<HEAD>
<TITLE>Scheme 48 Manual -- Sockets</TITLE>

</HEAD><BODY BGCOLOR="#ffffff">
<EM>Scheme 48 Manual</EM> | <A HREF="s48manual.html#top_node">Contents</A> | In Chapter: <A HREF="s48manual_35.html">Libraries</A><BR>Previous: <A HREF="s48manual_52.html">Sockets</A> | Next: <A HREF="s48manual_52.html">Sockets</A>
<H2>Sockets</H2>
Structure <CODE>sockets</CODE> provides access to TCP/IP sockets for interprocess
 and network communication.
<P><UL><LI><CODE>(open-socket<I></I>)&nbsp;-&gt;&nbsp;<I>socket</I></CODE><A NAME="1">&nbsp;</A>
<LI><CODE>(open-socket<I>&nbsp;port-number</I>)&nbsp;-&gt;&nbsp;<I>socket</I></CODE><A NAME="2">&nbsp;</A>
<LI><CODE>(socket-port-number<I>&nbsp;socket</I>)&nbsp;-&gt;&nbsp;<I>integer</I></CODE><A NAME="3">&nbsp;</A>
<LI><CODE>(close-socket<I>&nbsp;socket</I>)</CODE><A NAME="4">&nbsp;</A>
<LI><CODE>(socket-accept<I>&nbsp;socket</I>)&nbsp;-&gt;&nbsp;<I>input-port&nbsp;output-port</I></CODE><A NAME="5">&nbsp;</A>
<LI><CODE>(get-host-name<I></I>)&nbsp;-&gt;&nbsp;<I>string</I></CODE><A NAME="6">&nbsp;</A>
</UL>
<CODE>Open-socket</CODE> creates a new socket.
If no <CODE><I>port-number</I></CODE> is supplied the system picks one at random.
<CODE>Socket-port-number</CODE> returns a socket's port number.
<CODE>Close-socket</CODE> closes a socket, preventing any further connections.
<CODE>Socket-accept</CODE> accepts a single connection on <CODE><I>socket</I></CODE>, returning
 an input port and an output port for communicating with the client.
If no client is waiting <CODE>socket-accept</CODE> blocks until one appears.
<CODE>Get-host-name</CODE> returns the network name of the machine.
<P><UL><LI><CODE>(socket-client<I>&nbsp;host-name&nbsp;port-number</I>)&nbsp;-&gt;&nbsp;<I>input-port&nbsp;output-port</I></CODE><A NAME="7">&nbsp;</A>
</UL>
<CODE>Socket-client</CODE> connects to the server at <CODE><I>port-number</I></CODE> on
 the machine named <CODE><I>host-name</I></CODE>.
<CODE>Socket-client</CODE> blocks until the server accepts the connection.
<P>The following simple example shows a server and client for a centralized UID
 service.
<BLOCKQUOTE><PRE>
(define (id-server)
  (let ((socket (open-socket)))
    (display "Waiting on port ")
    (display (socket-port-number socket))
    (newline)
    (let loop ((next-id 0))
      (call-with-values
        (lambda ()
          (socket-accept socket))
        (lambda (in out)
          (display next-id out)
          (close-input-port in)
          (close-output-port out)
          (loop (+ next-id 1)))))))
         
(define (get-id machine port-number)
  (call-with-values
    (lambda ()
      (socket-client machine port-number))
    (lambda (in out)
      (let ((id (read in)))
        (close-input-port in)
        (close-output-port out)
        id))))
</PRE></BLOCKQUOTE>
<P><P>
  
Previous: <A HREF="s48manual_52.html">Sockets</A> | Next: <A HREF="s48manual_52.html">Sockets</A></BODY></HTML>
